﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebCommit.Common.Factories;
using WebCommit.Common.Interfaces;
using WebCommit.Web.Models;
using WebCommit.Common.Entitys;
using WebCommit.Common.Mvc.Filters;
using WebCommit.Common.Mvc.BaseClass;

namespace WebCommit.Web.Controllers
{
	/// <summary>
	/// 用户管理的控制器
	/// </summary>
	public class UserMgrController : BaseController
    {
		//[CheckLogin]
        public ActionResult Register()
        {
            return View();
        } 

        [HttpPost]
		[ValidateAntiForgeryToken]
		public ActionResult Register(RegisterModel model)
        {
			if (!this.ModelState.IsValid)
			{
				return View(model);
			}
            // TODO: Add insert logic here
			IUserSvc svc = SvcFactory.GetInstance<IUserSvc>();
			UserEntity user = model as UserEntity;
			svc.RegUser(user);
			return RedirectToAction("ReValidate", new { ValidateStatus = "RegSucess", UserId = user.Id, Email = user.Email });

        }
		//[HttpGet]
		public ActionResult IsUserNameExists(string userName)
		{ 
			bool rtn= SvcFactory.GetInstance<IUserSvc>().IsUserNameExists(userName);
			return Json(!rtn,JsonRequestBehavior.AllowGet);
		}
		public ActionResult IsNickNameExists(string nickName)
		{
			bool rtn = SvcFactory.GetInstance<IUserSvc>().IsNickNameExists(nickName);
			return Json(!rtn, JsonRequestBehavior.AllowGet);
		}
        
       
		public ActionResult RegisterSucess(RegisterModel model)
		{
			return View(model);
		}

		public ActionResult Login()
		{
			return View();
		}

		//[HttpPost]
		//[ValidateAntiForgeryToken]
		//public ActionResult Login(LoginModel model)
		//{
		//    UserEntity user = SvcFactory.GetInstance<IUserSvc>().Login(model.UserName, model.Password);
		//    if (user != null)
		//    {
		//        if (user.UserStatus == Common.Enums.UserStatus.Normal)
		//        {
		//            HttpCookie userName = new HttpCookie("UserName", model.UserName) { Expires = DateTime.Now + new TimeSpan(365, 0, 0, 0) };
		//            HttpCookie autologin = new HttpCookie("AutoLogin", model.AutoLogin.ToString()) { Expires = DateTime.Now + new TimeSpan(365, 0, 0, 0) };
		//            if (model.AutoLogin)
		//            {
		//                HttpCookie password = new HttpCookie("Password", model.Password) { Expires = DateTime.Now + new TimeSpan(365, 0, 0, 0) };
		//                Response.Cookies.Add(password);
		//            }
		//            Response.Cookies.Add(userName);
		//            Response.Cookies.Add(autologin);
		//            Session["LoginUser"] = user;
		//            model.IsLoginSucess = true;
		//            return Redirect(model.RedirctUrl);
		//        }
		//        else if (user.UserStatus == Common.Enums.UserStatus.Pending)
		//        {
		//            //return new UserController().Validate(user);

		//            Session["LoginUser"] = user;

		//            //ViewData.Add("ValidateStatus", "NeedValidate");
		//            return RedirectToAction("ReValidate", "UserMgr", new { ValidateStatus = "NeedValidate" });
		//        }
		//        else
		//        {
		//            return RedirectToAction("NotAllow", "UserMgr", new { Email = user.Email, Id = user.Id });
		//        }
		//    }
		//    else
		//    {
		//        model.IsLoginSucess = false;
		//        model.IsActLogin = true;
		//        model.Password = "";

		//        return RedirectToAction("Login", "UserMgr", model);
		//    }

		//}
		[HttpGet]
		public ActionResult ReValidate()
		{
			return View();
		}

		[HttpPost]
		public ActionResult ReValidate(dynamic model)
		{
			//UserEntity userInfo = Session["LoginUser"] as UserEntity;
			string email = model.Email;
			string userId = model.UserId;
			string newEmail = model.NewEmail;
			bool sucess = SvcFactory.GetInstance<IUserSvc>().ReSendValidateMail(email,userId,newEmail);
			if(sucess)
			{
				return RedirectToAction("ReValidate", new { ValidateStatus = "Resend", UserId = userId, Email = newEmail });
			}
			else
			{//todo:error
				return RedirectToAction("Login"); ;
			}
		}
		public ActionResult NotAllow(UserEntity user)
		{
			return View(user);
		}

		public ActionResult Validate(RegValidateEntity model)
		{
			object infoObj = new object();
			if (SvcFactory.GetInstance<IUserSvc>().ValidateUser(model, ref infoObj))
			{
				ViewData.Add("ValidateStatus", "Sucess");
				return View();
			}
			else
			{
				ViewData.Add("ValidateStatus", "Error");
				ViewData.Add("ValidateInfo", infoObj.ToString());
				return View();
			}
		}



    }
}
